#ifndef _SERVER_H
#define _SERVER_H

#include <map>
#include <unistd.h>
#include <stdio.h>  
#include <stdlib.h>  
#include <malloc.h>  
#include <ev.h>  
#include <netinet/in.h>  
#include <arpa/inet.h>  
#include <sys/socket.h>  
#include <sys/types.h>  
#include <string.h>
#include <string>
#include "Connection.hpp"
#include "ev++.h"
#include "Handler.hpp"

class accept_callback;
class client_callback;
class ServerManager;

class Server {
	friend class ServerManager;
	friend class accept_callback;
	friend class client_callback;
private:
	std::string name;                              //Server的名字
	int fd;                                        //Server对应的fd
	int clients_num;                               //服务器的连接数

	std::map<int, Connection *> ConnManager;       //Server的连接管理器	
	
	struct ev_loop *loop;                          //与Server绑定的事件循环
	client_callback *client_cb;                    //与Server绑定的客户端回调
	accept_callback *accept_cb;                    //与Server绑定的accept回调
	ev::io *socket_accept;                         //与Server的绑定的accpet监视器

	Server(std::string n, const char *ip_addr, int port); //该类对象只能通过管理器类获取
	Server(const Server &);                        //该类不允许复制
	const Server & operator= (const Server &);     //该类不允许赋值
	int SocketInit(const char *ip_addr, int port); //初始化socket

	Handler read_handler;
	Handler write_handler;

public:
	~Server();
	void start();
	int getClientsNum();
	void delConn(int fd);
	std::string getName() const;
};
#endif
